//
//  SKYIndex.m
//  SkyonSDK
//
//  Created by 张二帅 on 2018/5/5.
//  Copyright © 2018年 Skyon. All rights reserved.
//

#import "Skyon.h"
#import <SKYDevice.h>
#import <SKYApplication.h>
#import <SKYDeviceInfoList.h>
#import <SKYBattery.h>
#import "SKYInfoServer.h"
#import "SKYPageTrackerMonitor.h"
#import "SKYInfoAcquisitionInterface.h"

@implementation SKYAnalyticsConfig

- (id)initWithNormalConfig {
    if (self = [super init]) {
        _bCrashReportEnabled = YES;
        _regularReportPolicy = SKYRegularReport_Open;
        _viewPathReportEnabled = YES;
        _webViewReportEnabled = YES;
//        _appkey = @"App Store";
    }
    return self;
}

static SKYAnalyticsConfig *sky_config = nil;
+ (instancetype)sharedInstance {
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        sky_config = [[SKYAnalyticsConfig alloc] initWithNormalConfig];
    });
    return sky_config;
}

@end


@implementation Skyon

#pragma mark - 初始化采集

/**
 初始化信息采集模块
 
 @param configure 采集分析配置实例，参考该类定义
 */
+ (void)startWithConfigure:(SKYAnalyticsConfig *)configure;{
    [SKYInfoServer startInfoServerWithConfigure:configure];
}

#pragma mark - 设置

+ (void)setLogEnabled:(BOOL)value;{
    [SKYInfoServer sharedInstance].logEnabled = value;
}

+ (void)setRegularReportInterval:(int)second;{
    [SKYInfoServer sharedInstance].regularReportSecond = second;
}

+ (void)setInfoAcquisitionReportURLString:(NSString *)URLString {
    [SKYInfoServer sharedInstance].infoAcquisitionURLString = URLString;
}

+ (void)setInfoAcquisitionWifiNameList:(NSString *)wifiNameList;{
    [SKYInfoServer sharedInstance].wifiNameList = wifiNameList;
}

+ (void)setIgnoreTrackPage:(NSString *)pageName;{
    [[SKYPageTrackerMonitor sharedInstance].ignoreTrackPage setValue:@(YES) forKey:pageName];
}

+ (void)setIgnoreTrackPages:(NSArray <NSString *>*)pageNames;{
    [pageNames enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [[SKYPageTrackerMonitor sharedInstance].ignoreTrackPage setValue:@(YES) forKey:obj];
    }];
}

+ (void)keychainPasswordService:(NSString *)service account:(NSString *)account {
    [SKYDevice keychainPasswordService:service account:account];
}

+ (void)startLocation;{
    [SKYDevice startLocation];
}

+ (void)stopLocation;{
    [SKYDevice stopLocation];
}

+ (void)startBatteryMonitoring {
    [SKYBattery startMonitoring];
}

+ (void)stopBatteryMonitoring {
    [SKYBattery stopMonitoring];
}

#pragma mark - 自定义采集信息

+ (void)event:(NSString *)event;{
    [Skyon event:event eventCode:nil eventDescribe:nil];
}

+ (void)event:(NSString *)event eventCode:(NSString *)eventCode eventDescribe:(NSString *)eventDescribe;{
    [SKYInfoAcquisitionInterface sendCustomEventWithEvent:event eventCode:eventCode eventDescribe:eventDescribe];
}

/**
 提交指定采集信息
 
 @param param 采集信息参数
 @param completionHandler 提交完成回调，error == nil 代表提交成功，否则包含错误信息
 */
+ (void)reportInfoParameter:(NSArray <NSString *>*)param completion:(void (^)(NSError *error))completionHandler;{
    //针对参数，直接提交信息
    [SKYInfoAcquisitionInterface sendPaskets:param completion:completionHandler];
}


#pragma mark - info methods

+ (NSString *)deviceName;{
    return [SKYDevice getDeviceName];
}

+ (NSString *)deviceDefaultLanguage;{
    return [SKYDevice getDefaultLanguage];
}

+ (NSString *)deviceModel;{
    return [SKYDevice getDeviceModel];
}

+ (NSString *)deviceSystemName;{
    return [SKYDevice getSystemName];
}

+ (NSString *)telephonyCarrierName;{
    return [SKYDevice getCarrierName];
}

+ (NSString *)deveceSystemVersion;{
    return [SKYDevice getSystemVersion];
}

+ (NSString *)telephonyMCCAndMNC;{
    return [SKYDevice getMCCAndMNC];
}

+ (NSString *)deviceResolution;{
    return [SKYDevice getResolution];
}

+ (NSString *)deviceElapseBootTime;{
    return [SKYDevice getElapseBootTime];
}

+ (NSString *)currentAltitude;{
    return [SKYDevice getAltitudeInfo];
}

+ (NSString *)systemDateTime;{
    return [SKYDevice getSystemDateTime];
}

+ (BOOL)isLocationEnabled;{
    return [SKYDevice isLocationEnabled];
}

+ (NSString *)currentLocationInfo;{
    return [SKYDevice getLocationInfo];
}

+ (BOOL)isBluetoothSupported;{
    return [SKYDevice isBluetoothSupported];
}

+ (BOOL)isConnectWifi;{
    return [SKYDevice isConnectWifi];
}

+ (BOOL)isGyroscopeAvailable;{
    return [SKYDevice isGyroscopeAvailable];
}

+ (BOOL)isMobileDevice;{
    return [SKYDevice isMobile];
}

+ (NSString *)wifiMacAddress;{
    return [SKYDevice wifiMacAddress];
}

+ (NSString *)uniqueDeviceIdentifier;{
    return [SKYDevice uniqueDeviceIdentifier];
}

+ (NSString *)networkIP;{
    return [SKYInfoServer sharedInstance].netIp;
}

+ (NSString *)deviceDPI;{
    return [SKYDevice getDPI];
}

+ (NSString *)devicePPI;{
    return [SKYDevice getPPI];
}

+ (BOOL)isNFCAvailable;{
    return [SKYDevice isNFCAvailable];
}

+ (NSString *)deviceDiskSize;{
    return [SKYDevice getDiskSize];
}

+ (NSString *)deviceUsedDiskSize;{
    return [SKYDevice getUsedDiskSize];
}

+ (NSString *)deviceAvailableDiskSize;{
    return [SKYDevice getAvailableDiskSize];
}

+ (NSString *)deviceCoreInfo;{
    return [SKYDevice getCoreInfo];
}

+ (NSString *)cpuArchitectureDescription;{
    return [SKYDevice cpuArchitectureDescription];
}

+ (NSUInteger)cpuProcessorCount;{
    return [SKYDevice getCPUProcessorCount];
}

+ (BOOL)isJailbroken;{
    return [SKYDevice isJailbroken];
}

+ (NSString *)cpuFrequency;{
    return [NSString stringWithFormat:@"%@",kSKYCurrentCPUFrequency];
}

+ (NSString *)batteryStateDescribe;{
    return [SKYBattery getBatteryState];
}

+ (NSString *)batteryLevel;{
    return [SKYBattery getBatteryLevel];
}

+ (NSUInteger)batteryCapacity;{
    return [SKYBattery getBatteryCapacity];
}

+ (NSString *)applicationVersion;{
    return [SKYApplication getAppVersion];
}

+ (NSString *)applicationName;{
    return [SKYApplication getAppName];
}

+ (NSString *)applicationPackageName;{
    return [SKYApplication getAppPackageName];
}

+ (NSString *)advertisingIdentifier;{
    return [SKYDevice getIDFA];
}


@end
